package vip.meeet.algorithm.dp;


public class Num303 {

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        NumArray numArray = new NumArray(nums);
        System.out.println(numArray.sumRange(0, 2));
        System.out.println(numArray.sumRange(2, 5));
        System.out.println(numArray.sumRange(0, 5));
    }


    static class NumArray {

        private int[] dp;

        public NumArray(int[] nums) {
            if (nums.length == 0) {
                dp = new int[1];
                dp[0] = 0;
                return;
            }
            dp = new int[nums.length];
            dp[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                dp[i] = dp[i - 1] + nums[i];
            }

        }

        public int sumRange(int i, int j) {
            if(i==0){
                return dp[j];
            }
            if (i == j) {
                return dp[i] - dp[i - 1];
            }
            return dp[j] - dp[i - 1];
        }
    }

}

